// pages/authorization/authorization.js
const {
  ScanQRcode,
  Qrcodelength
} = require('../../common/js/utils')
let conf = require('../../app.config').app
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cdn: conf.cdnPath,
    act: '',
    open: true,
    scanTabHeight:0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options.act)
    this.setData({
      act: options.act,
    });
    this.getSystem();
  },
  getSystem(){
    let _this = this;
    wx.getSystemInfo({
      success: function(res) {
        var w = res.windowWidth;
        var h = res.windowHeight;
        var scanTabHeight = w * (1443/750);
        _this.setData({
          scanTabHeight:scanTabHeight,
        });
      },
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  // 获取授权
  onGetUserInfo(res) {
    let that = this
    if (!that.data.open) {
      return
    }
    that.setData({
      open: false
    })
    wx.getUserProfile({
      lang: 'zh_CN',
      desc: '微信用户信息授权',
      success(res) {
        that.setData({
          open: true
        })
        console.log('auth:', res)
        if (res.errMsg != 'getUserProfile:ok')
          wx.showToast({
            title: '请同意微信用户信息授权',
            icon: 'none',
            duration: 2000
          })
        else {
          console.log(res)
          let parm = {}
          if (res.encryptedData && res.iv)
            parm = {
              encryptedData: res.encryptedData,
              iv: res.iv,
              sourceStr: 'crm',
              userInfo: res.userInfo,
              // helpData: {
              //   customerId: wx.getStorageSync('shareopenid') || '',
              //   helpType: 'NEWUSER'
              // }
            }
          else
            parm = {
              userInfo: res.userInfo,
              sourceStr: 'crm',
              // helpData: {
              //   customerId: wx.getStorageSync('shareopenid') || '',
              //   helpType: 'NEWUSER'
              // }
            }
          that.api.interface.authNameAndHead(parm).then(res => {
            if (res.data.code == 200) {
              if (res.data.data.newUserAuthData) { // 恭喜获得新客优惠券
                wx.setStorageSync('newUserAuthData', res.data.data.newUserAuthData)
              }
              // that.api.login().then(res => {
              //   if (wx.getStorageSync('scancode')) {
              //     // 请求接口提交
              //     that.api.interface.getCustomerInfo().then(res => {
              //       wx.setStorageSync('userinfo', res.data.data)
              //     })
              //     that.api.interface.smallScanPoint({
              //       pinCode: wx.getStorageSync('scancode')
              //     }).then(result => {
              //       ScanQRcode(result)
              //     })
              //   } else if (that.data.act && that.data.act == 1) {
              //     wx.reLaunch({
              //       url: '/pages/home/home',
              //     })
              //   } else if (that.data.act && that.data.act == 2) {
              //     wx.reLaunch({
              //       url: '/pages/star/back',
              //     })
              //   } else if (that.data.act && that.data.act == 3) {
              //     // 送你1张优惠券 领取成功
              //     wx.reLaunch({
              //       url: '/pages/home/home?shareType=送你1张优惠券&shareGetType=授权完毕新用户领取成功&openid=' +  wx.getStorageSync('shareopenid')
              //     })
              //   }  else if (that.data.act && that.data.act == 4) {
              //     // 送你1份试用礼 领取成功
              //     wx.reLaunch({
              //       url: '/pages/home/home?shareType=送你1份试用礼&shareGetType=授权完毕新用户领取成功&friendOntrialOrderId=' +  wx.getStorageSync('friendOntrialOrderId')
              //     })
              //   }else {
              //     wx.navigateTo({
              //       url: '/pages/qna/result',
              //     })
              //   }
              // })
            
              wx.navigateTo({
                url: '/pages/messageauthorized/messageauthorized?act='+that.data.act,
              })
            } else
              app.alert(res.data.message)
          })
        }
      },
      fail(res) {
        that.setData({
          open: true
        })
        wx.showToast({
          title: '请同意名称头像授权',
          icon: 'none',
          duration: 2000
        })
        
      }
    });

  },
  // 返回上一页
  ongoback() {
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})